﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Tracy.DataModel;
using TracyGUI.View;
using TracyGUI.View.UIComponent;
using SharedLib;
using System.Diagnostics;

namespace TracyGUI.View.EntryTabs
{
    class TabPageFiles : TabPageGeneric<MediaFile>, IMediaFileTabPageView
    {
        public event EventHandler<EnhancedEventArgs> MediaFileExecuting;
        public event EventHandler<GenericEventArgs<MediaFile>> ContainingFolderOpening;
        public event EventHandler<GenericEventArgs<MediaFile>> OpenWithMenuGenerating;
        public event EventHandler<GenericEventArgs<List<MediaFile>>> MediaFilesChanging;
        public event EventHandler<GenericEventArgs<List<MediaFile>>> MediaFilesRemoving;
        

        ToolStripMenuItem _openWithMenu;

        private ToolStripMenuItem AddDropDownMenuItem(string name, string text, ToolStripMenuItem parent, EventHandler actionHandler)
        {
            ToolStripMenuItem item = new ToolStripMenuItem(text);
            item.Name = name;
            item.Click += actionHandler;
            parent.DropDownItems.Add(item);
            return item;
        }

        public TabPageFiles()
        {
            ListViewItems.MouseDoubleClick += new MouseEventHandler(ListViewItems_MouseDoubleClick);

            //Build Context Menu
            ListViewItems.ContextMenuStrip = new ContextMenuStrip();
            ListViewItems.ContextMenuStrip.Opening += new System.ComponentModel.CancelEventHandler(ContextMenuStrip_Opening);
            //Change Status
            ToolStripMenuItem changeStatusMenu = new ToolStripMenuItem(Tracy.TracyFacade.Instance.LanguageManager.GetText("MenuItem_ChangeStatus"));
            AddDropDownMenuItem("Ready", Tracy.TracyFacade.Instance.LanguageManager.GetText("MediaFileStatus_Ready"), changeStatusMenu, new EventHandler(ChangeStatusMenu_Click));
            AddDropDownMenuItem("Watched", Tracy.TracyFacade.Instance.LanguageManager.GetText("MediaFileStatus_Watched"), changeStatusMenu, new EventHandler(ChangeStatusMenu_Click));
            ListViewItems.ContextMenuStrip.Items.Add(changeStatusMenu);
            //Open Containing Folder
            ListViewItems.ContextMenuStrip.Items.Add(Tracy.TracyFacade.Instance.LanguageManager.GetText ("MenuItem_OpenContainingFolder")).Click += new EventHandler(OpenContainingFolderMenu_Click);
            //Open With
            _openWithMenu = new ToolStripMenuItem(Tracy.TracyFacade.Instance.LanguageManager.GetText("MenuItem_OpenWith"));
            _openWithMenu.DropDownOpening += new EventHandler(_openWithMenu_DropDownOpening);
            _openWithMenu.DropDownItems.Add("Loading");
            ListViewItems.ContextMenuStrip.Items.Add(_openWithMenu);
            //Remove
            ListViewItems.ContextMenuStrip.Items.Add(Tracy.TracyFacade.Instance.LanguageManager.GetText("MenuItem_Remove")).Click += new EventHandler(RemoveMenu_Click);
        }

        public void SetOpenWithMenu(List<string> displayNameList, List<ProcessStartInfo> appInfoList)
        {
            if(displayNameList == null) throw new ArgumentNullException("displayNameList");
            if(appInfoList == null) throw new ArgumentNullException("appInfoList");
            if(displayNameList.Count != appInfoList.Count) throw new ArgumentException("displayNameList count and appInfoList count are not equal");
            _openWithMenu.DropDownItems.Clear();
            for (int i = 0; i < displayNameList.Count; i++)
            {
                ToolStripItem openWithItem = _openWithMenu.DropDownItems.Add(displayNameList[i]);
                openWithItem.Tag = appInfoList[i];
                openWithItem.Click += new EventHandler(openWithItem_Click);
            }
        }

        void _openWithMenu_DropDownOpening(object sender, EventArgs e)
        {
            List<MediaFile> files = GetSelectedDataItems();
            if (files.Count > 0)
            {
                OpenWithMenuGenerating.InvokeEvent(this, new GenericEventArgs<MediaFile>(files[0]));
            }
        }

        void ContextMenuStrip_Opening(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (GetSelectedDataItems().Count == 0) e.Cancel = true;
        }

        void OpenContainingFolderMenu_Click(object sender, EventArgs e)
        {
            List<MediaFile> files = GetSelectedDataItems();
            if (files.Count > 0)
            {
                ContainingFolderOpening.InvokeEvent(this, new GenericEventArgs<MediaFile>(files[0]));
            }
        }

        void ChangeStatusMenu_Click(object sender, EventArgs e)
        {
            List<MediaFile> changedFiles = new List<MediaFile>();
            List<MediaFile> selectedFiles = GetSelectedDataItems();
            string newStatus = (((ToolStripItem)sender).Name);
            foreach (MediaFile file in selectedFiles)
            {
                if (file.Status != newStatus)
                {
                    file.Status = newStatus;
                    changedFiles.Add(file);
                }
            }

            if (changedFiles.Count > 0) MediaFilesChanging.InvokeEvent(this, new GenericEventArgs<List<MediaFile>>(changedFiles));
        }

        void ListViewItems_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            List<MediaFile> files = GetSelectedDataItems();
            if (files.Count > 0)
            {
                EnhancedEventArgs args = new EnhancedEventArgs();
                args.Attributes.Add("file", files[0]);
                args.Attributes.Add("processStartInfo", null);
                MediaFileExecuting.InvokeEvent(this, args);
            }
        }
        
        void openWithItem_Click(object sender, EventArgs e)
        {
            List<MediaFile> files = GetSelectedDataItems();
            if (files.Count > 0)
            {
                EnhancedEventArgs args = new EnhancedEventArgs();
                args.Attributes.Add("file", files[0]);
                args.Attributes.Add("processStartInfo", ((ToolStripItem)sender).Tag);
                MediaFileExecuting.InvokeEvent(this, args);
            }
        }

        void RemoveMenu_Click(object sender, EventArgs e)
        {
            List<MediaFile> files = GetSelectedDataItems();
            if (files.Count > 0)
            {
                MediaFilesRemoving.InvokeEvent(this, new GenericEventArgs<List<MediaFile>>(files));
            }
        }

        protected override void InitColumns()
        {
            //TODO: load from settings
            _listViewItems.AddColumn("Name", Tracy.TracyFacade.Instance.LanguageManager.GetText("ColumnName_Name"));
            _listViewItems.AddColumn("Type", Tracy.TracyFacade.Instance.LanguageManager.GetText("ColumnName_Type"));
            _listViewItems.AddColumn("Status", Tracy.TracyFacade.Instance.LanguageManager.GetText("ColumnName_Status"));
            _listViewItems.Columns["Name"].Width = 500;
        }

        protected override ListViewItem CreateListViewItem(MediaFile dataItem)
        {
            ListViewItem lvItemMediaFile = new ListViewItem();
            lvItemMediaFile.Name = dataItem.ID;
            lvItemMediaFile.Tag = dataItem;
            lvItemMediaFile.Text = dataItem.FileName;
            EnhancedListView.AddSubListViewItem(lvItemMediaFile, "Type", dataItem.GetFileExtension());
            EnhancedListView.AddSubListViewItem(lvItemMediaFile, "Status", Tracy.TracyFacade.Instance.LanguageManager.GetText("MediaFileStatus_" + dataItem.Status));
            if (dataItem.Status == "Ready") lvItemMediaFile.ForeColor = System.Drawing.Color.DarkRed;
            return lvItemMediaFile;
        }

        protected override void UpdateListViewItem(ListViewItem lvItem, MediaFile dataItem)
        {
            lvItem.SubItems["Status"].Text = Tracy.TracyFacade.Instance.LanguageManager.GetText("MediaFileStatus_" + dataItem.Status);
            if (dataItem.Status == "Ready") lvItem.ForeColor = System.Drawing.Color.DarkRed;
            else lvItem.ForeColor = System.Drawing.Color.Black;
        }

        protected override bool IsVisiable(MediaFile dataItem)
        {
            return (dataItem.Visible == "true");
        }

        #region ITabPageView Members


        public TabPage TabPage
        {
            get { return this; }
        }

        #endregion
    }
}
